<?php

class Default_Controllers_Cart extends Libs_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index() {
        $cart = new Default_Models_Product();
        $this->view->cartPro = $cart->getProductCart();
        $this->view->render('cart/index');
    }

    public function addToCart($pro_id) {
        $amount = $_POST['txtAmount'];
        @session_start();
        if (isset($_SESSION['cart'][$pro_id])) {
            $_SESSION['cart'][$pro_id] += $amount;
        } else {
            $_SESSION['cart'][$pro_id] = $amount;
        }

        $url = URL_BASE . "/cart";
//        $url = URL_BASE;
        if (!empty($_SERVER['HTTP_REFERER'])) {
            header("Location: " . $_SERVER['HTTP_REFERER']);
        } else {
            header("Location: $url");
        }
//        header("location: $url");
    }

    public function updateCart() {
        @session_start();
        $quantity = array();
        $quantity = $_POST['txtQuantity'];
        foreach ($quantity as $key => $value) {
            if ($value == 0 && is_numeric($value)) {
                unset($_SESSION['cart'][$key]);
            } elseif ($value > 0 && is_numeric($value)) {
                $_SESSION['cart'][$key] = $value;
            }
        }
//        if ($quantity > 0) {
//            $_SESSION['cart'][$pro_id] = $quantity;
//        }
        $url = URL_BASE . "/cart";
        header("location: $url");
    }

    public function delCart($pro_id) {
        @session_start();
        if (isset($pro_id)) {
            unset($_SESSION['cart'][$pro_id]);
        } else {
            unset($_SESSION['cart']);
        }
        $url = URL_BASE . "/cart";

        header("location: $url");
    }

    public function order() {
        $quantity = array();
        $quantity = $_POST['txtQuantity'];
        $this->view->orderPro = $quantity;
        $this->view->render('cart/order');
    }

}
